# for awx
#change source to china
echo "=================================================="
echo "=                                                ="
echo "=                Shell......Begin                ="
echo "=                                                ="
echo "=================================================="
sudo mv /etc/resolv.conf /etc/resolv.conf.bak
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo apt update

if [ "$HOSTNAME" = "awx" ]; then
    # install docker
    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    sh get-docker.sh

    if [ ! $(getent group docker) ]; then
        sudo groupadd docker
    fi

    sudo gpasswd -a $USER docker
    sudo service docker restart

    rm -rf get-docker.sh

    # install python venv
    sudo apt-get install -y python3-venv python3-pip
    # install docker-compose
    sudo python3 -m pip install --upgrade pip
    sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    sudo pip3 install docker-compose
    sudo pip3 install ansible==2.9.*
    pip3 install docker
    wget https://github.com/ansible/awx/archive/refs/tags/16.0.0.tar.gz
    tar -xvf 16.0.0.tar.gz
    cd ~/awx-16.0.0/installer
    sudo ansible-playbook -i inventory install.yml
fi

# open password auth for backup if ssh key doesn't work, bydefault, username=vagrant password=vagrant
# please comment these two lines if you want to disable username/password login
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo service sshd restart
sudo apt-get install net-tools -y
echo "=================================================="
echo "=                                                ="
echo "=                Shell......End                  ="
echo "=                                                ="
echo "=================================================="
